package com.bluestation.net.http;

/*****************************************************************
 *
 * Author: Partha Kuchana
 * Date : 23rd August 2001
 * Description: This class signifies a specific client request
 *
 * ClientRequest.java		1.0 	23/08/2001 	Partha Kuchana
 *
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this block appears in all copies. 
 *
 * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 ******************************************************************/


import java.net.*;
import java.io.*;
import java.util.*;

public class ClientRequest extends Thread {
  private Socket socket = null;
  private PrintStream pw = null;
  private DataOutputStream dos = null;
  private BufferedReader in = null;

      public static final String GET="GET";
	
      /**
      * Constructor for ClientRequest
      */
  public ClientRequest(Socket socket) throws Exception{
    super("ClientRequest");
    this.socket = socket;

    // Open output stream to the client
    try{
          pw = new PrintStream(socket.getOutputStream(), true);
          dos = new DataOutputStream(socket.getOutputStream());        
          in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));
    } catch (IOException e) {
          throw e;
    }
  }

  public void run() {
    @SuppressWarnings("unused") String inputLine, outputLine;
    String action;
    String fileName=null;
    StringTokenizer st=null;

    try {
          // Verify,process the current request
      if ((inputLine = in.readLine()) != null) {
        st=new StringTokenizer(inputLine);
        action=(String)st.nextElement();
        if (st.hasMoreElements()){
          fileName=(String)st.nextElement();
        }
        if (action.equals(ClientRequest.GET)){
          transmitResource(fileName);
        }
      }
    
    } catch (Exception e) {
          WSUtil.log("Unable to process request for the resource " + fileName + "\n" +
            " Exception = " + e.getMessage());
    }
    finally{
          try{				
            pw.close();
            in.close();
            socket.close();				
          }catch(Exception e){
          //
          }
    }// finally
    WSUtil.log( " Resource " + fileName + " sent successfully to the client ");
  }//run

/**
  * This method sends the resource to the client
  */
  private void transmitResource(String fileName) throws Exception{
    DataInputStream dis=null;
    byte b;
	
    File f=new File(WebServer.HOME_DIRECTORY + fileName);
    try{
      dis=new DataInputStream(new FileInputStream(f));

          //Send header to the client
          sendHeader(fileName);

          //Send the actual resource            
      for (int x=0;x<f.length();x++){
        b=dis.readByte();
        dos.writeByte(b);
      }
    } catch (Exception e) {
          throw e;
    }
    finally{
          try{
            dis.close();        
          }catch(Exception e){
            //
          }
    }
  }

/**
  * This method sends the header to the client
  * @param resourceName java.lang.String
  */
private void sendHeader(String resourceName) throws Exception{
    String header=null;	
    String contentType=null;
        
    try{
          File f=new File(WebServer.HOME_DIRECTORY + resourceName);

          if (resourceName.endsWith(".html")){
            contentType="text/html";
          }
          if (resourceName.endsWith(".jpg")){
            contentType="image/jpeg";
          }
          if (resourceName.endsWith(".jpeg")){
            contentType="image/jpeg";
          }
          String contentLength=Integer.toString((int)f.length(),10);
          header="HTTP/1.0 200 OK\n" +
            "Allow: GET\n" + 
            "MIME-Version: 1.0\n" +
            "Content-type: " + contentType + "\n" +
            "Content-length: " + contentLength + "\n" +
            "\n";
          pw.print(header);
        }catch(Exception e){
          throw e;
        }	
      }

}
